package com.zhouxiaoge.https.base;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * @author zmy
 */
public class MyHandler implements HttpHandler {
    @Override
    public void handle(HttpExchange httpExchange) throws IOException {
        System.out.println(httpExchange.getRequestURI().toString());


        InputStream is = httpExchange.getRequestBody();
        byte[] temp = new byte[is.available()];
        is.read(temp);
        System.out.println(new String(temp));

        String response = "<h1>Hello World!</h1>";
        httpExchange.sendResponseHeaders(200, response.length());
        OutputStream os = httpExchange.getResponseBody();
        os.write(response.getBytes());
        os.close();
    }
}
